<template>
    <div class='dialog'>
        <div class='newAdd dialog-con add-platform-container'>
            <split-vertical title="新增平台信息"></split-vertical>
            <div class="form">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="110px" class="demo-ruleForm" :label-position="labelPosition">
                <el-form-item label="平台性质" prop="platformNature">
                  <el-radio-group v-model="ruleForm.platformNature" @change="selectNature">
                    <el-radio label="A">银行</el-radio>
                    <el-radio label="B">电商</el-radio>
                  </el-radio-group>
                </el-form-item>
                  <el-form-item label="平台名称" prop="platformName">
                    <el-input v-model="ruleForm.platformName"></el-input>
                  </el-form-item>
                  <el-form-item label="合作公司" prop="companyId">
                    <el-select v-model="ruleForm.companyId" placeholder="请选择" size="medium"  filterable allow-create clearable style="width: 96%;">
                      <el-option
                        v-for="item in list"
                        :key="item.id"
                        :label="item.companyName"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item class="btn" >
                    <el-button @click="submitForm('ruleForm')" type="primary"  :disabled="isChannelBtn" size="medium">提交</el-button>
                    <el-button @click="cancel"  size="medium" style="margin-right: 20px;">取消</el-button>
                  </el-form-item>
              </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import SplitVertical from "components/common/SplitVertical"
import {localStorage} from 'js/localStorage'
export default {
  inject: ["reload"],
  components:{
    SplitVertical
  },
  data() {
    return {
      //创建按钮是否disabled
      isChannelBtn:false,
      labelPosition:'left',
      list:[],
      platformNature:'',
      ruleForm: {
        platformNature:'',
        platformName: "",
        companyId: ""

      },
      rules: {
        platformNature: [
          { required: true, message: '请选择一个平台性质', trigger: 'blur' }
        ],
        platformName: [
          { required: true, message: '请输入平台名称', trigger: 'blur' },
          { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
        ],
        companyId: [
          { required: true, message: '请选择合作公司', trigger: 'change' }
        ],
        channelContact: [
          { required: true, message: '请输入渠道联系人', trigger: 'change' },
          { min: 2, max: 5, message: '长度在 2到 5 个字符', trigger: 'blur' }
        ],
        channelMobile: [
          { required: true, message: '请输入联系人电话', trigger: 'change' },
          {min: 11, max:11,message: '请输入正确的手机号', trigger: 'blur'}
        ]
      }
    };
  },
  created(){
    this.userId = localStorage.get("userID")
    this.getAllCompany()
  },
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.confirm()
          } else {
            return false;
          }
        });
      },
    selectNature(val){
      this.platformNature=val
    },
    getAllCompany(){
      let success=data=>{
        if(data.statusCode == '1'){
          this.list= data.body
        }else{
          this.$message.error(data.msg)
        }
      }
      this.api.getCompanyListByNature({},success)
    },
    confirm() {
        let parmas = this.ruleForm
        parmas['userId']=this.userId
        let succ = data => {
          if (data.statusCode === "1") {
            console.log("##,===")
            this.$message.success("添加信息成功");
            this.reload();
            this.$emit("closeDialog");
          } else {
            this.$message.error(data.statusMessage);
          }
        };
       this.api.platformAdd(parmas, succ)
    },
    cancel() {
      this.$emit("closeDialog");
    }
  }
};
</script>
<style scoped>
  .add-platform-container .el-input{
    width:250px;
  }
  .add-platform-container .el-form-item__content{
    margin-left:0 !important
  }
</style>
<style lang="scss" scoped>
.add-platform-container {
    width: 400px;
    height: 300px;
  .btn{
     width:100%;
     text-align: right;
     padding-top: 20px;
      margin-right: 20px;
    }
  .form{
    margin-left:15px;
  }

}
</style>

